from rest_framework.serializers import ModelSerializer,SerializerMethodField
from user.serializers import UserSerializer
from school.models import Classes



class ClassesSerializer(ModelSerializer):
    # member = UserSerializer(many=True)
    member_detail = SerializerMethodField() # 获取get_member方法返回的值

    class Meta:
        model = Classes
        exclude = ['is_delete']

        extra_kwargs={
            'member':{'write_only':True} # 只写==反序列化
        }

    def get_member_detail(self,classes):
        serializer=UserSerializer(classes.member.all(),many=True)
        data={
            # 分组，老师组id为1 学生组id为2
            'teacher':[i for i in serializer.data if 1 in i['groups'] or i['is_superuser']],
            'student':[i for i in serializer.data if 2 in i['groups']],

        }
        return data

